"""
Created on 21 mrt. 2012

@author: Daan
"""
import unittest
import os
import shutil

from pCommon import Configuration

TEST_DIR = os.path.join(os.path.dirname(__file__), "dat")
UNITTEST_OUTPUT_DIR = os.path.join(TEST_DIR, "output")

class ConfigurationTester(unittest.TestCase):

    def setUp(self):
        if not os.path.isdir(UNITTEST_OUTPUT_DIR):
            os.makedirs(UNITTEST_OUTPUT_DIR)

    def tearDown(self):
        shutil.rmtree(UNITTEST_OUTPUT_DIR, True)

    def test_01_instantiation(self):
        
        testObject = Configuration.Configuration()
        
        self.assertTrue(isinstance(testObject, Configuration.Configuration))


    def test_02_empty_default(self):

        testObject = Configuration.Configuration(default = {})

        try:
            _ = testObject.aSection.aValue
            self.assertFalse(True)
        except Configuration.SectionError as e:
            self.assertEqual("aSection does not exist", e.message)

    def test_03a_normal_empty_default(self):

        testObject = Configuration.Configuration(os.path.join(UNITTEST_OUTPUT_DIR, "config.ini"), default = {})

        try:
            _ = testObject.aSecondSection.aValue
            self.assertFalse(True)
        except Configuration.SectionError as e:
            self.assertEqual("aSecondSection does not exist", e.message)

    def test_03b_normal(self):

        testObject = Configuration.Configuration(os.path.join(UNITTEST_OUTPUT_DIR, "config.ini"), default = { "aSection" : {"aValueName" : "aValue"}})

        self.assertEqual("aValue", testObject.aSection.aValueName)

if __name__ == "__main__":
    unittest.main()